class Photo < ApplicationRecord
  mount_uploader :image, PhotoUploader
  include Slugable
  include Rails.application.routes.url_helpers

  belongs_to :user

  def to_jq_upload
    {
      name: read_attribute(:name),
      type: read_attribute(:format),
      size: read_attribute(:size),
      url: self.image_url(),
      thumbnail_url: self.image_url(),
      delete_url: 'no',
      delete_type: 'DELETE'
    }
  end

  before_save :set_banner_attributes

  private

  def set_banner_attributes
    if image.present? && image_changed?
      self.name = image.file.filename
      self.size = image.file.size
      self.format = image.file.content_type
    end
  end
end
